package com.wayhome.srpingbootmybatis.mbs.tblyrule.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wayhome.srpingbootmybatis.mbs.tblyrule.dao.TbdyRuleDao;
import com.wayhome.srpingbootmybatis.mbs.tblyrule.dto.DrgSetlAvgCostNewDTO;
import com.wayhome.srpingbootmybatis.mbs.tblyrule.dto.DrgSetlAvgDaysNewDTO;
import com.wayhome.srpingbootmybatis.mbs.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class DrgSetlAvgDaysNewListener extends AnalysisEventListener<DrgSetlAvgDaysNewDTO> {

    private AtomicInteger successCnt = new AtomicInteger(0);
    private List<DrgSetlAvgDaysNewDTO> list = new ArrayList<>(Constants.MAX_LIST_SIZE);

    public List<DrgSetlAvgDaysNewDTO> getList() {
        return list;
    }

    private final TbdyRuleDao tbdyRuleDao;

    public DrgSetlAvgDaysNewListener(TbdyRuleDao tbdyRuleDao) {
        this.tbdyRuleDao = tbdyRuleDao;
    }

    private List<String> genSaveSqlList = new ArrayList<>();
    private static String DOUHAO_SPLIT = "', ";
    private static String SET_SPLIT = "' ";
    private static String SET_END = "';";
    private static String INSERT_END = "');";

    @Override
    public void invoke(DrgSetlAvgDaysNewDTO drgSetlAvgDaysNewDTO, AnalysisContext analysisContext) {
        if (drgSetlAvgDaysNewDTO == null) {
            return;
        }
        Date now = new Date();
        drgSetlAvgDaysNewDTO.setValiFlag("1");
        drgSetlAvgDaysNewDTO.setCrteTime(now);
        drgSetlAvgDaysNewDTO.setUpdtTime(now);

        BigDecimal sumDays = drgSetlAvgDaysNewDTO.getSumDays();
        if (sumDays == null) {
            drgSetlAvgDaysNewDTO.setSumDays(BigDecimal.ZERO);
        } else {
            drgSetlAvgDaysNewDTO.setSumDays(sumDays.setScale(2, RoundingMode.HALF_UP));
        }
        BigDecimal avgDays = drgSetlAvgDaysNewDTO.getAvgDays();
        if (avgDays == null) {
            drgSetlAvgDaysNewDTO.setAvgDays(BigDecimal.ZERO);
        } else {
            drgSetlAvgDaysNewDTO.setAvgDays(avgDays.setScale(2, RoundingMode.HALF_UP));
        }

        genSql(drgSetlAvgDaysNewDTO);
//        list.add(drgSetlAvgDaysNewDTO);
//        if (list.size() >= Constants.MAX_LIST_SIZE) {
//            int i = tbdyRuleDao.batchInsertDrgSetlAvgDaysNew(list);
//            System.out.println("导入DIP次均费用（没解析百条会触发一下），成功条数：" + successCnt.addAndGet(i));
//            list.clear();
//        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("Drg平均住院天数导入调用完成");
        if (!CollectionUtils.isEmpty(list)) {
            int i = tbdyRuleDao.batchInsertDrgSetlAvgDaysNew(list);
            log.info("Drg平均住院天数最后一次入库，条数为:{}, 最终导入成功条数:{}", list.size(), successCnt.addAndGet(i));
            list.clear();
        }

    }

    private void genSql(DrgSetlAvgDaysNewDTO drgSetlAvgDaysNewDTO) {
        DrgSetlAvgDaysNewDTO dbData = tbdyRuleDao.queryDrgSetlAvgDaysNew(drgSetlAvgDaysNewDTO);

        StringBuilder stringBuilder  = new StringBuilder();
        if (!Objects.isNull(dbData)) {
            stringBuilder.append("update drg_setl_avg_days_new_d set SUM_DAYS = '")
                    .append(drgSetlAvgDaysNewDTO.getSumDays()).append(DOUHAO_SPLIT)
                    .append("SETL_CNT = '")
                    .append(drgSetlAvgDaysNewDTO.getSetlCnt()).append(DOUHAO_SPLIT)
                    .append("AVG_DAYS = '")
                    .append(drgSetlAvgDaysNewDTO.getAvgDays()).append(SET_SPLIT)
                    .append(" where id = '")
                    .append(dbData.getId()).append(SET_END);
        } else {
            stringBuilder.append("INSERT INTO drg_setl_avg_days_new_d (SUM_DAYS,SETL_CNT,AVG_DAYS,SETL_LV,CLR_OPTINS," )
                    .append("DRG_CODG,DRG_NAME, SETL_YEAR) VALUE ('")
                    .append(drgSetlAvgDaysNewDTO.getSumDays()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getSetlCnt()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getAvgDays()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getSetlLv()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getClrOptins()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getDrgCodg()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getDrgName()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgDaysNewDTO.getSetlYear()).append(INSERT_END);

        }
        System.out.println(stringBuilder.toString());
        genSaveSqlList.add(stringBuilder.toString());

    }
}
